<?php

namespace BXTR\AdminBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Routing\Route;
use Doctrine\Common\Util\Inflector;
use Doctrine\Common\Util\ClassUtils;
/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class BXTRAdminExtension extends Extension
{
    const ROUTE_EDIT = '_edit';
    const ROUTE_ADD = '_edit';
    const ROUTE_LIST = '_edit';
    const ROUTE_REMOVE_POPUP = '_edit';
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        // @TODO Check validity of entities linked in categories
        foreach ($config['linklist']['categories'] as $categoryArray) {
            foreach($categoryArray['entities'] as $entName) {
                if (!array_key_exists($entName, $config['entities'])) {
                    throw new \Exception('The entity "' . $entName . '" is listed in bxtr_admin.linklist.categories but was not found in the bxtr_admin.entities configuration.');
                }
            }
        }

        $routePrefix = 'bxtr_admin_';
        $controllersConfiguration = array();
        foreach ($config['entities'] as $entityName => $params) {

            foreach ($params['l18n'] as $transName => $transValue) {
                // if no value has been set in configuration
                if ($transValue === null) {
                    // set the empty translation to `Ent.{entity}.{key}`
                    // the entity name is uppercased and the array key camelcased.
                    $config['entities'][$entityName]['l18n'][$transName] = 'Ent.' . \ucfirst($entityName) . '.' . \ucfirst(Inflector::camelize($transName)); 
                }
            }
            foreach($params['routes'] as $routeName => $route) {
                if ($route === null) {
                    // prepend `_` to the route key to build the route name

                    $config['entities'][$entityName]['routes'][$routeName] = $routePrefix . $entityName . '_' . $routeName;
                }

                $controllersConfiguration[] = array(
                    // grab routes directly from $config array because it has been parsed but
                    // the $params has not.
                    'routes' => $config['entities'][$entityName]['routes'],
                    'controller' => $params['controller'],
                    'route' => $entityName,
                    'entity' => $entityName
                );
            }
        }
        $container->setParameter('bxtr_admin.categories', $config['linklist']['categories']);
        $container->setParameter('bxtr_admin.entities', $config['entities']);
        // Add ConfigLoader service to configuration and pass entities to convert to routes.
        $configLoaderDefinition = new Definition('BXTR\AdminBundle\Routing\ConfigLoader');
        $configLoaderDefinition
            ->addArgument($controllersConfiguration)
            ->addTag('routing.loader', array());
        $container->setDefinition('loader_routing', $configLoaderDefinition); 

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader
            ->load('services.yml');
    }
}
